
#include "SceneEditor.h"
#include "SceneImageLoader.h"
#include "QDictionary.h"
#include "QArray.h"
#include "ImageInfo.h"


SceneImageLoader::SceneImageLoader(const QString &imagePath, const QString &filePath)
    : mImagePath(imagePath)
    , mFilePath(filePath)
{

}

SceneImageLoader::~SceneImageLoader()
{

}

void SceneImageLoader::run()
{
    emit started();

    if (mFilePath.isEmpty())
    {
        return;
    }

    // 读取图像配置文件
    QDictionary dict;

    QFile file(mFilePath);
    if (!file.open(QIODevice::ReadOnly))
    {
        QString msg = QString(tr("Open file ")) + mFilePath + QString(tr(" failed !"));
        emit finished(-1, msg);
        return;
    }

    bool result = dict.unarchive(&file);
    if (!result)
    {
        file.close();
        QString msg = QString(tr("Read file ")) + mFilePath + QString(tr(" failed !"));
        emit finished(-1, msg);
        return;
    }

    file.close();

    // 读取配置文件中，图像文件数量
    int numberOfImages = dict.value(SCN_IMG_COUNT_KEY).toInt();
    // 背景图像数量
    int numberOfBkgnd = dict.value(SCN_IMG_BKGND_COUNT_KEY).toInt();
    // 遮挡图数量
    int numberOfMask = dict.value(SCN_IMG_MASK_COUNT_KEY).toInt();

    emit totalSteps(numberOfImages);

    // 加载背景图
    QVariant var = dict.value(SCN_IMG_BKGND_LIST_KEY);
    QArray bkgndList = var.value<QArray>();
    int count = bkgndList.size();

    if (numberOfBkgnd != count)
    {
        emit finished(-1, tr("Invalid number of background images !"));
        return;
    }

    int step = 0;

    QArray::iterator itr = bkgndList.begin();
    while (itr != bkgndList.end())
    {
        QVariant var = *itr;
        const QDictionary &imageInfo = var.value<QDictionary>();
        QString filename = imageInfo.value(SCN_IMG_NAME_KEY).toString();
        int x = imageInfo.value(SCN_IMG_X_KEY).toInt();
        int y = imageInfo.value(SCN_IMG_Y_KEY).toInt();
        ImageInfo *info = new ImageInfo(mImagePath, filename);
        info->setPosition(QPoint(x, y));
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(info->image());
        ++step;
        emit progress(step, info, ImageBkgnd, item);
        ++itr;
    }

    // 加载遮挡图
    QArray maskList = dict.value(SCN_IMG_MASK_LIST_KEY).value<QArray>();
    count = maskList.size();

    if (numberOfMask != count)
    {
        emit finished(-1, tr("Invalid number of mask images !"));
        return;
    }

    itr = maskList.begin();
    while (itr != maskList.end())
    {
        const QDictionary &imageInfo = (*itr).value<QDictionary>();
        QString filename = imageInfo.value(SCN_IMG_NAME_KEY).toString();
        int x = imageInfo.value(SCN_IMG_X_KEY).toInt();
        int y = imageInfo.value(SCN_IMG_Y_KEY).toInt();
        ImageInfo *info = new ImageInfo(mImagePath, filename);
        info->setPosition(QPoint(x, y));
        QGraphicsPixmapItem *item = new QGraphicsPixmapItem(info->image());
        ++step;
        emit progress(step, info, ImageMask, item);
        ++itr;
    }

    emit finished(0, tr("Image loading completed !"));
}
